package com.cskaoyan.javase.practice.stage2;

import java.util.Scanner;

/**
 * @program: little-practice
 * @description: a String list store student info.
 * @author: Keyu Li
 * @create: 2021-08-28 20:08
 **/

public class StudentManageSystemBeta {
    public static void main(String[] args) {
        // create a student list that could contain 10 sstudents
        String[] stu = new String[10];
        Scanner sc = new Scanner(System.in);
        int menuNum;

        while (true) {
            printMenu();
            System.out.println("请选择功能（1-6）：");
            menuNum = Integer.parseInt(sc.nextLine());

            if (menuNum == 1) {
                showStudentList(stu);
            } else if (menuNum == 2) {
                System.out.println("input a student name to add: ");
                String temp = sc.nextLine();
                if (!addStudentInfo(stu, temp)) {
                    System.out.println("student list is full and cannot add " + temp + " !");
                }
            } else if (menuNum == 3) {
                System.out.println("input a student name to delete: ");
                String temp = sc.nextLine();
                if (!deleteStudentInfo(stu, temp)) {
                    System.out.println("couldn't find student " + temp + " !");
                }
            } else if (menuNum == 4) {
                System.out.println("input a student name to update: ");
                String temp = sc.nextLine();
                if (!updateStudentInfo(stu, temp, sc)) {
                    System.out.println("couldn't find student " + temp + " !");
                }
            } else if (menuNum == 5) {
                System.out.println("input a student name to search: ");
                String temp = sc.nextLine();
                int index = searchStudentInfo(stu, temp);
                if (index == -1) {
                    System.out.println("couldn't find student " + temp + " !");
                } else {
                    System.out.println("student " + temp + " index is " + index);
                }
            } else if (menuNum == 6) {
                System.out.println("确认退出吗（1/2）：");
                if (Integer.parseInt(sc.nextLine()) == 1)
                    return;
            } else {
                System.out.println("input illegal!");
            }
        }
    }

    private static void showStudentList(String[] stu) {
        System.out.print("[ ");
        boolean notEmpty = false;
        for (int i = 0; i < stu.length; i++) {
            if (stu[i] == null)
                continue;
            System.out.print(stu[i] + ", ");
            notEmpty = true;
        }
        if (notEmpty)
            System.out.print("\b");
        System.out.println("\b ]");
    }

    private static boolean addStudentInfo(String[] stu, String temp) {
        for (int i = 0; i < stu.length; i++) {
            if (stu[i] == null) {
                stu[i] = temp;
                return true;
            }
        }
        return false;
    }

    private static boolean deleteStudentInfo(String[] stu, String temp) {
        for (int i = 0; i < stu.length; i++) {
            if (stu[i] != null && stu[i].equals(temp)) {
                stu[i] = null;
                return true;
            }
        }
        return false;
    }

    private static boolean updateStudentInfo(String[] stu, String temp, Scanner sc) {
        for (int i = 0; i < stu.length; i++) {
            if (stu[i] != null && stu[i].equals(temp)) {
                System.out.println("input new name: ");
                stu[i] = sc.nextLine();
                return true;
            }
        }
        return false;
    }

    private static int searchStudentInfo(String[] stu, String temp) {
        for (int i = 0; i < stu.length; i++) {
            if (stu[i] != null && stu[i].equals(temp)) {
                return i;
            }
        }
        return -1;
    }

    private static void printMenu() {
        System.out.println("-------------王道Java学生管理系统---------");
        System.out.println();
        System.out.println("              1.学 生 列 表");
        System.out.println("              2.增 加 学 生");
        System.out.println("              3.删 除 学 生");
        System.out.println("              4.修 改 学 生");
        System.out.println("              5.查 询 学 生");
        System.out.println("              6.退 出 系 统");
    }
}
